//
//  MainController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/8.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainController: UITabBarController {
    
    @objc private func editBtnClick () {
        
        let vc =  EditViewController()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: false, completion: nil)
        
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let mainTabBar = MainTabBar()
        
        //KVC
        //由于 tabBar 是只读的 不能直接操作 如果想要修改 就使用KVC
        setValue(mainTabBar, forKey: "tabBar")
        
        //添加子视图控制器
        addChildViewControllers()
        
        //按钮的点击事件
        mainTabBar.editBtn.addTarget(self, action: "editBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    private func addChildViewControllers() {
        
        addChildViewControllers(HomeController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewControllers(MessageController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewControllers(DiscoveryController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewControllers(ProfileController(), title: "我的", imageName: "tabbar_profile")
        
        
    }
    
    //重载
    private func addChildViewControllers(vc:UIViewController,title:String,imageName:String){
        
        
        self.tabBar.tintColor = UIColor.orangeColor()
        
        //实例化导航控制器
        let nav  = BaseNavController(rootViewController:vc)
        
        //标题是由内 向 外 设置的
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(nav)
        
        
    }
    
}
